package kk.controller;

import com.github.pagehelper.PageInfo;
import com.sun.media.sound.SoftTuning;
import kk.domain.Role;
import kk.domain.UserInfo;
import kk.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/findAll")
   @PreAuthorize("hasRole('ROLE_ADMIN')")
    public ModelAndView findAll(Integer pageNum,Integer pageSize) throws Exception {
        ModelAndView modelAndView=new ModelAndView();
        List<UserInfo> userInfoList = userService.findAll(pageNum,pageSize);
        PageInfo pageInfo=new PageInfo(userInfoList);
        modelAndView.addObject("pageInfo",pageInfo);
        modelAndView.setViewName("user-list");
        return modelAndView;
    }

    @RequestMapping("/save")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public String save(UserInfo userInfo) throws Exception{
        userService.save(userInfo);
        return "redirect:/user/findAll?pageNum=1&pageSize=4";
    }

    @RequestMapping("/findById")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public ModelAndView findById(Integer id)throws Exception{
        UserInfo userInfo = userService.findById(id);
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("user",userInfo);
        modelAndView.setViewName("user-show");
        return modelAndView;
    }

    @RequestMapping("/findUserByIdAndCanAddRole")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public ModelAndView findUserByIdAndCanAddRole(@RequestParam(name = "id",required = true) Integer userId) throws Exception {
        UserInfo userInfo = userService.findById(userId);
        List<Role> otherRoles = userService.findOtherRoles(userId);
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("user",userInfo);
        modelAndView.addObject("roleList",otherRoles);
        modelAndView.setViewName("user-role-add");
        return modelAndView;
    }

    @RequestMapping("/findUserByIdAndCanDeleteRole")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public ModelAndView findUserByIdAndCanDeleteRole(@RequestParam("id") Integer userId) throws Exception{
        UserInfo userinfo = userService.findById(userId);
        List<Role> existRoles = userService.findExistRoles(userId);
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("user",userinfo);
        modelAndView.addObject("roleList",existRoles);
        modelAndView.setViewName("user-role-delete");
        return modelAndView;
    }


    @RequestMapping("/addRoleToUser")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public String addRoleToUser(Integer userId,int[] ids) throws Exception {
        userService.addRoleToUser(userId,ids);
        return "redirect:/user/findAll?pageNum=1&pageSize=4";
    }

    @RequestMapping("/deleteById")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public String deleteById(Integer id) throws Exception{
        userService.deleteById(id);
        return "redirect:/user/findAll?pageNum=1&pageSize=4";
    }


    @RequestMapping("/deleteRoleFromUser")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public String deleteRoleFromUser(Integer userId,int[] ids) throws Exception {
        userService.deleteRoleFromUser(userId,ids);
        return "redirect:/user/findAll?pageNum=1&pageSize=4";
    }

    @RequestMapping("/deleteSelectedUser")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public String deleteSelectedUser(int[] ids) throws Exception{
        for (int id : ids) {
            userService.deleteById(id);
        }
        return "redirect:/user/findAll?pageNum=1&pageSize=4";
    }
}
